/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.entity.tower;

import java.io.IOException;
import java.util.Hashtable;
import javax.microedition.lcdui.Image;
import towerz.game.entity.BulletData;
import towerz.system.BitmapFont;
import towerz.system.StringManager;
import towerz.system.Utilities;
import towerz.system.xml.XmlNode;

/**
 *
 * @author LegACy
 */
public class TowerData {
	public TowerData() {
		//Initialize
		m_Sell				= Utilities.TOWER_SELL;
		m_Cost				= 0;
		m_Index				= 0;
		m_Range				= 0;
		m_Reload			= 0;
		m_Image				= null;
		m_Bullet			= null;
		m_RangeArea			= null;
		m_ImageFile			= "";
		m_NameTable			= new Hashtable();
		m_DescriptionTable	= new Hashtable();
		
		//No image
		m_CostString		= null;
		
	}

	public TowerData(XmlNode xml) {
		//Default
		this();

		//Get index
		m_Index = Integer.parseInt(xml.getAttribute("index"));

		//For each data
		for (int i = 0; i < xml.getChildren().size(); i++) {
			//Get variable
			String Tag		= ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();
			String Value	= ((XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(0)).getNodeValue();

			//Check name
			if (Tag.equals("bullet"))	m_Bullet = new BulletData((XmlNode)xml.getChildren().elementAt(i));
			else if (Tag.equals("reload"))	m_Reload = Integer.parseInt(Value);
			else if (Tag.equals("sell"))	m_Sell = Float.parseFloat(Value);	
			else if (Tag.equals("cost")) {
				m_Cost			= Integer.parseInt(Value);
				m_CostString	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(Value + " " + StringManager.instance().getString("culture"), 0x00000000);
			}
			else if (Tag.equals("image")) {
				//Create image
				m_ImageFile = Value;
				try {
					m_Image = Image.createImage(Utilities.TOWER_FOLDER		+ "/" + m_ImageFile + ".png");
					m_Icon	= Image.createImage(Utilities.TOWER_ICON_FOLDER	+ "/" + m_ImageFile + ".png");
				} catch (IOException ex) {}
			} else if (Tag.equals("range"))	{
				//Set range
				m_Range				= Integer.parseInt(Value);
				long RangeSquare	= m_Range * m_Range;

				//Create RGB data
				int[] AreaRGB = new int[m_Range * m_Range * 4];
				for (int x = 0; x < m_Range * 2; x++) {
					for (int y = 0; y < m_Range * 2; y++) {
						//Get distance to center
						long Distance2 = ((x - m_Range) * (x - m_Range)) + ((y - m_Range) * (y - m_Range));

						//If in range, draw
						if (Distance2 <= RangeSquare) AreaRGB[(y * m_Range * 2) + x] = Utilities.ATTACKAREA_COLOR;
					}
				}

				//Create image
				m_RangeArea = Image.createRGBImage(AreaRGB, m_Range * 2, m_Range * 2, true);
			}
		}
	}
	
	public void loadDescription(XmlNode xml) {
		//Initialize
		int i				= 0;
		
		//For each data
		while (i < xml.getChildren().size()) {
			//Get variable
			String Tag		= ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();

			//Based on tag name
			if( Tag.equals("names")) {
				//Init
				int j = 0;
				
				//For each child
				while (j < ((XmlNode)xml.getChildren().elementAt(i)).getChildren().size()) {
					//Get child
					XmlNode Child = (XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(j);
					
					//If name
					if (Child.getNodeName().equals("name"))
						m_NameTable.put(Child.getAttribute("language"), BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((XmlNode)Child.getChildren().elementAt(0)).getNodeValue(), 0x00000000));
					
					//Next
					j++;
				}
			}
			else if (Tag.equals("descriptions")) {
				//Init
				int j = 0;
				
				//For each child
				while (j < ((XmlNode)xml.getChildren().elementAt(i)).getChildren().size()) {
					//Get child
					XmlNode Child = (XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(j);
					
					//If name
					if (Child.getNodeName().equals("description"))
						m_DescriptionTable.put(Child.getAttribute("language"), BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((XmlNode)Child.getChildren().elementAt(0)).getNodeValue(), 0x00000000));
					
					//Next
					j++;
				}
			}
			
			//Next 
			i++;
		}
	}

	//Accessors
	public int getCost()			{	return m_Cost;					}
	public int getSell()			{	return (int) (m_Cost * m_Sell);	}
	public int getRange()			{	return m_Range;					}
	public Image getIcon()			{	return m_Icon;					}
	public long getReload()			{	return m_Reload;				}
	public Image getImage()			{	return m_Image;					}
	public Image getRangeArea()		{	return m_RangeArea;				}
	public Image getCostString()	{	return m_CostString;			}
	public String getImageFile()	{	return m_ImageFile;				}
	public BulletData getBullet()	{	return m_Bullet;				}
	
	public Image getNameString() {
		return (Image)m_NameTable.get(StringManager.instance().getTag());
	}
	
	public Image getDescString() {
		return (Image)m_DescriptionTable.get(StringManager.instance().getTag());
	}

	//Data
	protected int		m_Index;
	protected int		m_Cost;
	protected float		m_Sell;
	protected Image		m_Icon;
	protected Image		m_Image;
	protected String	m_ImageFile;

	//Attack data
	protected int			m_Range;
	protected long			m_Reload;
	protected Image			m_RangeArea;
	protected BulletData	m_Bullet;
	
	//Bitmap text
	protected Image m_CostString;
	protected Hashtable m_NameTable;
	protected Hashtable m_DescriptionTable;
}
